﻿/**************************************************************

项目名称 :ThinkWide数据访问基础库
版权所有 (c) 2009 ThinkWide
+-------------------------------------------------------------
项目: 数据接口服务类


版本: 1


创建日期:2009-5-3


作者: 章称

+-------------------------------------------------------------
描述:

更新:   2009-5-3

ToDo:

***************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MF.Core;
using System.Xml;

namespace MF.Data.Manager
{

    public class MFXmlConfiguration
    {
        private string FConnectString;
        private string FSupplyClass;
        private string FSupplyDllPath;

        public string Name
        {
            get;
            set;
        }

        public string ConnectString
        {
            get {return FConnectString;}
            set {FConnectString=value;}
        }

        public string SupplyClass
        {
            get { return FSupplyClass; }
            set { FSupplyClass = value; }

        }

        public string SupplyDllPath
        {
            get { return FSupplyDllPath; }
            set { FSupplyDllPath = value; }

        }

        public string GetSupplyDllRealPath()
        {
            if (!SupplyDllPath.Contains(":"))
            {
                if (AppDomain.CurrentDomain.RelativeSearchPath != null)
                    return AppDomain.CurrentDomain.RelativeSearchPath + "\\" + SupplyDllPath;
                else

                    return AppDomain.CurrentDomain.BaseDirectory + SupplyDllPath;
            }
            return SupplyDllPath;
        }

    }

    public class MFXmlConfigurations :List<MFXmlConfiguration> 
    {
        public const string  cProviderName="Name";
        public const string cConnectionString="ConnectionString" ;
        public const string cSupplyClass="SupplyClass";
        public const string cSupplyDllPath = "SupplyDllPath";
        public const string cDefaultProvider = "DefaultProvider";

        public MFXmlConfigurations(string sXmlPath)
        {

            sXmlPath = MFPath.GetRealPath(sXmlPath);

            if (File.Exists(sXmlPath))
            {
                XmlDocument myDoc = new XmlDocument();
                try
                {
                    myDoc.Load(sXmlPath);
                    XmlNode Root = myDoc.DocumentElement;
                    foreach (XmlNode ANode in Root.ChildNodes)
                    {
                        MFXmlConfiguration Item = new MFXmlConfiguration();
                        Item.Name = ANode.Attributes[cProviderName].Value;

                        foreach (XmlNode BNode in ANode.ChildNodes)
                        {
                            if (MFString.ComparerNoCase(BNode.Name, cSupplyDllPath)) Item.SupplyDllPath = BNode.InnerText;

                            if (MFString.ComparerNoCase(BNode.Name, cSupplyClass)) Item.SupplyClass = BNode.InnerText;

                            if (MFString.ComparerNoCase(BNode.Name, cConnectionString)) Item.ConnectString = BNode.InnerText;
                        }

                        this.Add(Item);
                    }
                }
                catch
                {
                    throw;
                }

                
            }
        }

        public MFXmlConfiguration GetDefaultProvider()
        {
            return GetProvider(cDefaultProvider);
        }

        public MFXmlConfiguration GetProvider(string sProviderName)
        {

            foreach(MFXmlConfiguration Item in this)
            {
                if (String.Compare(Item.Name, sProviderName, true) == 0)
                {
                    return Item;
                }
            }

            return null;
        }

    }
}
